package com.alipay.app.groupmeal.standard.widget.business;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @ProjectName: 团餐
 * @ClassName: BScreenRecyclerView
 * @Description: B屏专用RecyclerView, 针对小键盘按键事件做处理
 * @Author: bd-001935
 * @CreateDate: 4/15/22 1:35 PM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/15/22 1:35 PM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class BScreenRecyclerView extends RecyclerView {
    public BScreenRecyclerView(@NonNull Context context) {
        this(context, null);
    }

    public BScreenRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BScreenRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //取消item的动画，防止item刷新产生闪烁
        ((DefaultItemAnimator) getItemAnimator()).setSupportsChangeAnimations(false);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            View s = null;
            if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
                //下一个
                s = focusSearch(findFocus(), View.FOCUS_DOWN);
                if (s != null) {
                    s.requestFocus();
                }
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
                //上一个
                s = focusSearch(findFocus(), View.FOCUS_UP);
                if (s != null) {
                    s.requestFocus();
                }
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                //确认
            }
        }
        return super.dispatchKeyEvent(event);
    }

}
